

package com.shop.cloud.mall.api.job;

import com.shop.cloud.common.core.constant.SecurityConstants;
import com.shop.cloud.common.data.tenant.TenantContextHolder;
import com.shop.cloud.mall.api.service.SeckillHallService;
import com.shop.cloud.mall.common.entity.SeckillHall;
import com.shop.cloud.upms.common.entity.SysTenant;
import com.shop.cloud.upms.common.feign.FeignTenantService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static com.xxl.job.core.biz.model.ReturnT.SUCCESS;

/**
 * 秒杀会场相关定时任务
 * @author
 * @date 2020-09-18
 */
@Slf4j
@Component
@AllArgsConstructor
public class SeckillHallHandler {

	private final SeckillHallService seckillHallService;
	private final FeignTenantService feignTenantService;

	/**
     * 定时更新指定会场的会场日期
	 * @param
     * @return
     */
	@XxlJob("seckillHallHallDateJobHandler")
	public ReturnT<String> seckillHallHallDateJobHandler(String s) {
		//查出所有租户
		List<SysTenant> listSysTenant = feignTenantService.list(SecurityConstants.FROM_IN).getData();
		listSysTenant.forEach(item -> {
			TenantContextHolder.setTenantId(item.getId());
			String[] seckillHallIds = new String[]{"1817759985495867394","1817760052596342785","1817760483707879425"};
			SeckillHall seckillHall;
			LocalDateTime time=LocalDateTime.now();
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
			String strDate = dtf.format(time);
			for(int i = 0; i<seckillHallIds.length; i++){
				seckillHall = new SeckillHall();
				seckillHall.setId(seckillHallIds[i]);
				seckillHall.setHallDate(strDate);
				seckillHallService.updateById(seckillHall);
			}
			TenantContextHolder.clear();
		});
		return SUCCESS;
	}

}